package demo15_2;

// 2、无头双向链表实现
public class MyLinkedList {

    static class ListNode {
        private int val;
        private ListNode prev;
        private ListNode next;
        public ListNode() {}

        public ListNode(int val) {
            this.val = val;
        }

    }
    public ListNode head;//双向链表的第一个节点
    public ListNode last;//双向链表的最后一个节点

    public void createLinkedList() {
        ListNode node1 = new ListNode(12);
        ListNode node2 = new ListNode(34);
        ListNode node3 = new ListNode(56);
        ListNode node4 = new ListNode(78);
        ListNode node5 = new ListNode(91);
        node1.next = node2;
        node2.next = node3;
        node3.next = node4;
        node4.next = node5;
        node5.prev = node4;
        node4.prev = node3;
        node3.prev = node2;
        node2.prev = node1;
        this.head = node1;
        this.last = node5;
    }
    //头插法
    public void addFirst(int data){
        ListNode node = new ListNode(data);
        if (head == null) {
            head = node;
            last = node;
        }else {
            node.next = this.head;
            this.head.prev = node;
            this.head = node;
        }
    }
    //尾插法
    public void addLast(int data){
        ListNode node = new ListNode(data);
        if (head == null) {
            head = node;
            last = node;
        }else {
            last.next = node;
            node.prev = last;
            last = last.next;
        }
    }
    //任意位置插入,第一个数据节点为0号下标
    public void addIndex(int index,int data){
        checkIndex(index);
        if (index == 0) {
            addFirst(data);
            return;
        }
        if (index == size()) {
            addLast(data);
            return;
        }

        ListNode node = new ListNode(data);
        ListNode cur = head;
        for (int i = 0; i < index; i++) {
            cur = cur.next;
        }
        node.next = cur;
        cur.prev.next = node;
        node.prev = cur.prev;
        cur.prev = node;
    }
    private void checkIndex(int index) {
        if (index < 0 || index > size()) {
            throw new IndexErrorException("index不合法");
        }
    }


    //删除第一次出现关键字为key的节点
    //要删除的是中间节点
    //要删除的是最后一个节点
    //要删除的是第一个节点且链表只有头节点
    //要删除的是第一个节点且链表有多个节点
    public void remove(int key){
        ListNode cur = head;
        while (cur != null) {
            if (cur.val == key) {
                break;
            }
            cur = cur.next;
        }
        if (cur == null) {
            System.out.println("没有要删除的节点");
            return;
        }
        //cur指向要删除的节点
        if (cur.prev != null && cur.next != null) {
            cur.prev.next = cur.next;
            cur.next.prev = cur.prev;
        }else if (cur == head) {
            head = head.next;
            //只有一个节点
            if (head != null) {
                head.prev = null;
            }else {
                last = null;
            }
        }else if (cur == last){

            last = last.prev;
            last.next = null;
        }

    }


    //删除所有值为key的节点
    public void removeAllKey(int key){
        ListNode cur = head;
        while (cur != null) {
            if (cur.val == key) {
                if (cur.prev != null && cur.next != null) {
                    cur.prev.next = cur.next;
                    cur.next.prev = cur.prev;
                }else if (cur == head) {
                    head = head.next;
                    //只有一个节点
                    if (head != null) {
                        head.prev = null;
                    }else {
                        last = null;
                    }
                }else if (cur == last){
                    last = last.prev;
                    last.next = null;
                }
                cur = cur.next;
            }
        }
    }

    //查找是否包含关键字key是否在链表当中
    public boolean contains(int key){
        ListNode cur = this.head;
        while (cur != null) {
            if (cur.val == key) {
                return true;
            }
            cur = cur.next;
        }
        return false;
    }
    //得到链表的长度
    public int size(){
        ListNode cur = this.head;
        int count = 0;
        while (cur != null) {
            count++;
            cur = cur.next;
        }
        return count;
    }
    public void display(){
        ListNode cur = this.head;
        while (cur != null) {
            System.out.print(cur.val + " ");
            cur = cur.next;
        }
        System.out.println();
    }

    public void clear(){
        ListNode cur = head;
        while (cur != null) {
            ListNode curNext = cur.next;
             cur.prev = null;
             cur.next = null;
             cur = curNext;
        }
        head = null;
        last = null;
    }
}